package com.cloudcreativity.blyg.ui.cashier;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.base.BaseActivity;
import com.cloudcreativity.blyg.databinding.ActivityCashierBinding;
import com.cloudcreativity.blyg.utils.ScanGunHelper;

public class CashierAC extends BaseActivity {

    private ScanGunHelper scanGunHelper;
    private CashierModel model;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCashierBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_cashier);
        model = new CashierModel(this, binding, this);
        binding.setModel(model);
        scanGunHelper = new ScanGunHelper(new ScanGunHelper.OnScanValueListener() {
            @Override
            public void onScanValue(String value) {
                // LogUtils.e("xuxiwu",value);
                model.onCode(value);
            }
        });
        String scanCode = getIntent().getStringExtra("scanCode");
        if(!TextUtils.isEmpty(scanCode)){
            model.onCode(scanCode);
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // LogUtils.e("xuxiwu","device id = "+event.getDeviceId());
        if ((event.getKeyCode() != KeyEvent.KEYCODE_BACK) && (event.getDeviceId()!=-1)) {
            scanGunHelper.analysisKeyEvent(event);
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(model!=null){
            model.onDestroy();
        }
    }
}
